package com.dcivision.framework.taglib.html;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.html.BaseFieldTag;
import org.apache.struts.taglib.html.BaseHandlerTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import com.dcivision.framework.TextUtility;

public class BaseHandler {

  public static final String REVISION = "$Revision: 1.8.26.1 $";
  
  private static final Log log = LogFactory.getLog(BaseHandler.class);
  
  public static final String ONCHANGE_JS_FUNCTION = "OpSaveFieldModified(this);";
  public static final String ONSUBMIT_JS_FUNCTION = ";return(false)";
  public static final int DIRECT_SKIP_BODY = 1;
  public static final int DIRECT_EVAL_BODY_BUFFERED = 2;
  public static final int RETURN_SUPER_DOSTART = 3;

  private BaseHandler() {
  }

  public static int commonRoutine(BaseFieldTag bfTag, PageContext pageContext, boolean isTextOnly) throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(bfTag, FormTag.class);

    // Show text with a hidden field if it is set to show text only or the parent
    // form is set to show text only.
    if (isTextOnly || (formTag!=null && formTag.getTextOnly())) {
      String sOutVal = "";
      if (bfTag.getValue() != null) {
        sOutVal = ResponseUtils.filter(bfTag.getValue());
      } else if (bfTag.getRedisplay() || !"password".equals(bfTag.getValue())) {
        Object value = RequestUtils.lookup(pageContext, bfTag.getName(), bfTag.getProperty(), null);
        if (value==null) {
          value = "";
        }
        sOutVal = ResponseUtils.filter(value.toString());
      }
      try {
        JspWriter out = pageContext.getOut();
        out.print(sOutVal);
        out.println("<input type='hidden' name='" + bfTag.getProperty() + "' value='" + sOutVal + "'>");
      } catch (Exception e) {
        log.error(e, e);
      }
      return(DIRECT_SKIP_BODY);
    }

    // Add the javascript control to trace whether the form fields have been
    // modified or not if necessary.
    if (formTag!=null && formTag.getTraceChange()) {
      String originalOnchange = bfTag.getOnchange();
      originalOnchange = (originalOnchange == null) ? "" : originalOnchange;
      bfTag.setOnchange(ONCHANGE_JS_FUNCTION + originalOnchange);
    }

    // Set this tag to be disabled if the Form has set to be disabled.
    if (!bfTag.getDisabled()) {
      if(formTag!=null){
        bfTag.setDisabled(formTag.getDisabled());
      }
    }

    return(RETURN_SUPER_DOSTART);
  }

  public static int commonRoutine(BaseHandlerTag bfTag, PageContext pageContext, boolean isTextOnly) throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(bfTag, FormTag.class);

    // Add the javascript control to trace whether the form fields have been
    // modified or not if necessary.
    if (formTag!=null && formTag.getTraceChange()) {
      String originalOnchange = bfTag.getOnchange();
      originalOnchange = (originalOnchange == null) ? "" : originalOnchange;
      bfTag.setOnchange(ONCHANGE_JS_FUNCTION + originalOnchange);
    }

    // Show text with a hidden field if it is set to show text only or the parent
    // form is set to show text only.
    if (isTextOnly || (formTag!=null && formTag.getTextOnly())) {
      if (bfTag instanceof TextareaTag) {
        String sOutVal = "";
        Object value = RequestUtils.lookup(pageContext, ((TextareaTag)bfTag).getName(), ((TextareaTag)bfTag).getProperty(), null);
        if (value==null) {
          value = "";
        }
        sOutVal = ResponseUtils.filter(value.toString());
        try {
          JspWriter out = pageContext.getOut();
          out.print(TextUtility.wrapParagraph(sOutVal));
          out.println("<input type='hidden' name='" + ((TextareaTag)bfTag).getProperty() + "' value='" + sOutVal + "'>");
        } catch (Exception e) {
          log.error(e, e);
        }
        return (DIRECT_SKIP_BODY);
      } else if (bfTag instanceof SelectTag) {
        return (DIRECT_EVAL_BODY_BUFFERED);
      } else {
        bfTag.setOnclick("return(false);");
        bfTag.setOnchange("return(false);");
        bfTag.setOnselect("return(false);");
        return (RETURN_SUPER_DOSTART);
      }
    }

    // Set this tag to be disabled if the Form has set to be disabled.
    if (!bfTag.getDisabled()) {
      if(formTag!=null){
        bfTag.setDisabled(formTag.getDisabled());
      }
    }

    return(RETURN_SUPER_DOSTART);
  }
}